package com.lln.cjcx.util.okhttp
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
import yfdc.GsonFactory
/**
 * @author LiLinnan
 * @version 1.0
 * @date 2020/1/22 20:33
 */
public final object CookieJarImpl : CookieJar {
	@JvmStatic public fun getInstance():CookieJarImpl=this
	private val cookieStore = HashMap<String, List<Cookie>>()
	public final override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
		println(this.toString())
		val cookies1 = cookieStore[url.host]
		if (cookies1 !== null) {
			cookieStore[url.host] = cookies1
			return
		}
		val cookieArrayList = ArrayList<okhttp3.Cookie>()
		cookieArrayList.addAll(cookies)
		cookieStore[url.host] = cookieArrayList
	}
	public final override fun loadForRequest(url: HttpUrl): List<Cookie> = cookieStore[url.host] ?: ArrayList<Cookie>()
	override fun toString(): String {
		return GsonFactory.getGson().toJson(cookieStore, java.util.Map::class.java)
	}
}
